package org.monster.context.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Servlet方法模板
 *
 * @author Monster
 */
public abstract class LifeCycleTemplate implements LifeCycle {

    /**
     * http服务者
     */
    private static HttpServlet servlet = null;
    /**
     * 类
     */
    private Class<?> clazz = null;

    /**
     * 构造方法
     */
    public LifeCycleTemplate() {
        super();
    }

    /**
     * 构造方法
     *
     * @param servlet http服务者
     */
    public LifeCycleTemplate(HttpServlet servlet) {
        LifeCycleTemplate.servlet = servlet;
    }

    /**
     * 获取 销毁方法
     *
     * @param clazz 类
     */
    public static void getDestoryMethod(Class<?> clazz) {
        try {
            Method method;
            method = clazz.getDeclaredMethod("destroy");
            method.invoke(servlet);
        } catch (NoSuchMethodException | SecurityException
                | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException ex) {
        }
    }

    /**
     * 获取 服务者配置
     *
     * @param clazz 类
     * @return 服务者配置
     */
    public static ServletConfig getServletConfig(Class<?> clazz) {
        ServletConfig sc = null;
        Method method;
        try {
            method = clazz.getDeclaredMethod("getServletConfig");
            sc = (ServletConfig) method.invoke(servlet);
        } catch (NoSuchMethodException | SecurityException
                | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException ex) {
        }
        return sc;
    }

    /**
     * 获取 初始方法
     *
     * @param clazz 类
     * @return 服务者
     */
    public static Method getInitMethod(Class<?> clazz) {
        Method init = null;
        try {
            init = clazz.getDeclaredMethod("init", ServletConfig.class);
        } catch (NoSuchMethodException | SecurityException ex) {
        }
        return init;
    }

    /**
     * 获取 执行方法名
     *
     * @param requestType 请求类型
     * @return 执行方法名
     */
    public static String getExecuteMethodName(String requestType) {
        String doMethod = "do";
        String fist = requestType.substring(0, 1).toUpperCase();
        String end = requestType.substring(1, requestType.length())
                .toLowerCase();
        return doMethod + fist + end;
    }

    /**
     * 获取 执行方法
     *
     * @param clazz             类
     * @param executeMethodName 执行方法名
     * @return 执行方法
     */
    public static Method getExecuteMethod(Class<?> clazz, String executeMethodName) {
        Method method = null;
        try {
            method = clazz.getDeclaredMethod(executeMethodName, HttpServletRequest.class,
                    HttpServletResponse.class);
        } catch (NoSuchMethodException | SecurityException ex) {
        }
        return method;
    }

    /**
     * 获取 服务方法
     *
     * @param clazz 类
     * @return 服务方法
     */
    public static Method getServiceMethod(Class<?> clazz) {
        Method method = null;
        try {
            method = clazz.getDeclaredMethod("service",
                    HttpServletRequest.class, HttpServletResponse.class);
        } catch (NoSuchMethodException | SecurityException ex) {
        }
        return method;
    }

    /**
     * 获取 类
     *
     * @return 类
     */
    public Class<?> getClazz() {
        return clazz;
    }

    /**
     * 设置 类
     *
     * @param clazz 类
     */
    public void setClazz(Class<?> clazz) {
        this.clazz = clazz;
    }

    /**
     * 获取 http服务者
     *
     * @return http服务者
     */
    public HttpServlet getServlet() {
        return servlet;
    }

    /**
     * 设置 http服务者
     *
     * @param servlet http服务者
     */
    public void setServlet(HttpServlet servlet) {
        LifeCycleTemplate.servlet = servlet;
    }

}
